package hczc.longcai.com.hccrowdfunding.home.second_invest.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import hczc.longcai.com.hccrowdfunding.R;
import hczc.longcai.com.hccrowdfunding.application.BaseApplication;
import hczc.longcai.com.hccrowdfunding.home.second_invest.activities.PayActivity;

/**
 * A simple {@link Fragment} subclass.
 */
public class DetailsFootFragment extends Fragment implements View.OnClickListener, View.OnTouchListener {


    private static final String TAG = "DetailsFootFragment";
    /**
     * 收藏按钮
     */
    private ImageView like;
    /**
     * 分享按钮
     */
    private ImageView share;
    /**
     * 减少一份
     */
    private ImageView mimus;
    /**
     * 增加一份
     */
    private ImageView plus;
    /**
     * 显示份数
     */
    private TextView copies;
    /**
     * 立即购买
     */
    private TextView buy;

    /**
     * 每份的金额
     */
    private int each = 100;

    public DetailsFootFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_details_foot, container, false);
        BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));
        initViews(view);

        return view;
    }

    private void initViews(View view) {
        like = ((ImageView) view.findViewById(R.id.details_foot_like));
        like.setOnClickListener(this);
        share = ((ImageView) view.findViewById(R.id.details_foot_share));
        share.setOnClickListener(this);
        mimus = ((ImageView) view.findViewById(R.id.details_foot_mimus));
        mimus.setOnClickListener(this);
        mimus.setOnTouchListener(this);
        plus = ((ImageView) view.findViewById(R.id.details_foot_plus));
        plus.setOnClickListener(this);
        plus.setOnTouchListener(this);
        plus.setSelected(true);
        copies = ((TextView) view.findViewById(R.id.details_foot_copies));
        copies.setOnClickListener(this);
        buy = ((TextView) view.findViewById(R.id.details_foot_buy));
        buy.setOnClickListener(this);
    }

    private int getCopies(){
        String s1 = copies.getText().toString();
        String[] s = s1.split("份");
        String c = s[0];
        Integer c_copies = Integer.valueOf(c);
//        Log.d(TAG,"c = "+c);
        return c_copies;
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        int c_copies = getCopies();
        switch (id){
            /**
             * 喜欢按钮的点击
             */
            case R.id.details_foot_like:
                Toast.makeText(getActivity(),"点击了喜欢按钮",Toast.LENGTH_SHORT).show();
                break;
            /**
             * 分享按钮的点击
             */
            case R.id.details_foot_share:
                Toast.makeText(getActivity(),"点击了分享按钮",Toast.LENGTH_SHORT).show();
                break;
            /**
             * 减号按钮的点击
             */
            case R.id.details_foot_mimus:
//                mimus.setSelected(true);
//                plus.setSelected(false);
                if(c_copies == 1){
                    Toast.makeText(getActivity(),"最小份数为1",Toast.LENGTH_SHORT).show();
                    break;
                }
                c_copies--;
                copies.setText(String.valueOf(c_copies)+"份");
                break;
            /**
             * 加号按钮的点击
             */
            case R.id.details_foot_plus:
//                mimus.setSelected(false);
//                plus.setSelected(true);
                c_copies++;
                copies.setText(String.valueOf(c_copies)+"份");
                break;
            /**
             * 份数的点击
             */
            case R.id.details_foot_copies:

                break;
            /**
             * 立即购买按钮的点击
             */
            case R.id.details_foot_buy:
                Intent intent = new Intent(getActivity(), PayActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("copies",c_copies);
                bundle.putInt("each",each);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
        }
    }



    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        view.setPressed(true);
        return false;
    }
}
